using System.ServiceModel;
using System.ServiceModel.Dispatcher;

namespace XcoAppSpaces.Transport.WCF
{
    /// <summary>
    /// Send session-ID when channel is closed
    /// </summary>
    public class XcoWCFInputSessionShutdown : IInputSessionShutdown
    {
        /// <summary>
        /// Interface for shutdown procedure
        /// </summary>
        public IXcoWCFTransportSessionShutdown Shutdown { get; set; }
        /// <summary>
        /// Create ShutdownBehavior instance
        /// </summary>
        /// <param name="shutdown">Interface for shutdown procedure</param>
        public XcoWCFInputSessionShutdown(IXcoWCFTransportSessionShutdown shutdown)
        {
            Shutdown = shutdown;
        }
        /// <summary>
        /// Not used
        /// </summary>
        /// <param name="channel"></param>
        public void ChannelFaulted(IDuplexContextChannel channel)
        {
            
        }

        /// <summary>
        /// Channel closed, send sessionID to shutdown cleanup
        /// </summary>
        /// <param name="channel"></param>
        public void DoneReceiving(IDuplexContextChannel channel)
        {
            //log.Debug("channel done: " + channel.LocalAddress);
            //log.Debug(String.Format("btw, sessionid = {0}", channel.SessionId));
            if (Shutdown != null && channel != null)
            {
                Shutdown.Shutdown(channel.SessionId);
            }
        }
    }
}